package com.antler.smt.portal.service.impl;

import com.antler.smt.constant.DateTimeConstant;
import com.antler.smt.entity.SupplierEntity;
import com.antler.smt.entity.SupplierPictureEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.*;
import com.antler.smt.request.portal.GetSupplierPictureReq;
import com.antler.smt.response.admin.GetSupplierListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.response.portal.GetImgResp;
import com.antler.smt.response.portal.GetSupplierPictureResp;
import com.antler.smt.service.portal.SupplierImgService;
import com.antler.smt.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 服务商图片Service
 *
 * @author zhangyi
 * @date 2018/5/12 12:16
 */
@Service
public class SupplierImgServiceImpl implements SupplierImgService {

    @Autowired
    private SupplierPictureEntityMapper supplierPictureEntityMapper;

    @Autowired
    private SupplierPictureInfoMapper supplierPictureInfoMapper;

    @Autowired
    private SupplierInfoMapper supplierInfoMapper;

    /**
     * 插入服务商的图片
     *
     * @param supplierPicture
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = DateTimeConstant.MINUTE_S, rollbackFor = Exception.class)
    public Result<ResultCodeEnum> insertSupplierImg(GetSupplierPictureReq supplierPicture,Long memberId) {
        Result result = null;
        //创建服务商图片实体
        SupplierPictureEntity supplierPictureEntity = new SupplierPictureEntity();

        SupplierEntity supplierEntity = new SupplierEntity();
        supplierEntity.setMemberId(memberId);
        GetSupplierListResp supplierListResp =
                supplierInfoMapper.selectSupplierByKey(supplierEntity);

        //插入服务商id
        supplierPictureEntity.setSupplierId(supplierListResp.getId());

        //申请未通过
        if(supplierListResp.getSettledAuditResult().equals(3)){
            List<String> generalImgList = supplierPictureInfoMapper.getGeneralImgList(supplierListResp.getId());
            if(generalImgList.size() >= 0) {
                int i = supplierPictureInfoMapper.deleteSupplierPicture(supplierListResp.getId());
                if (0 == i) {
                    result = new Result(ResultCodeEnum.APPEND_DATA_ERROR, "删除失败！");
                } else {
                    result = new Result(ResultCodeEnum.OPERATION_SUCCESS, "删除成功！");
                }
            }
        }

        for(int i = 0;i<supplierPicture.getAttachmentId().size();i++) {
            //插入文件id
            supplierPictureEntity.setAttachmentId(supplierPicture.getAttachmentId().get(i));

            //插入类型
            supplierPictureEntity.setType(supplierPicture.getType());

            //插入更新时间
            supplierPictureEntity.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));
            supplierPictureEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));

            int k = supplierPictureEntityMapper.insertSelective(supplierPictureEntity);

            if(0 == k){
                result = new Result(ResultCodeEnum.APPEND_DATA_ERROR,"插入失败！");
            }else{
                result = new Result(ResultCodeEnum.OPERATION_SUCCESS,"插入成功！");
            }
        }

        return result;
    }

    /**
     * 获取当前服务商下所有图片信息
     *
     * @param supplierId
     * @return
     */
    @Override
    public Result getSupplierImg(Long supplierId) {
        Result result = null;
        GetImgResp imgResp = null;

        List<GetImgResp> imgList = new ArrayList<>();

        List<String> generalImgList = supplierPictureInfoMapper.getGeneralImgList(supplierId);

        for(String imgName: generalImgList){
            imgResp = new GetImgResp();
            imgResp.setImgName(imgName);
            imgList.add(imgResp);
        }

        List<GetImgResp> supplierListInfo = supplierPictureInfoMapper.getSupplierListInfo(supplierId);

        Object[] listObject = new Object[]{imgList,supplierListInfo};
        result = new Result(listObject);

        return result;
    }
}
